package sms

import (
	"fmt"
	"github.com/gin-gonic/gin"

	"vrcm/src/vrcm/app/admin/service/dto/sms"
	smsService "vrcm/src/vrcm/app/admin/service/sms"
	"vrcm/src/vrcm/common/apis"
)

type Sms struct {
	apis.Api
}

// SendSmsCode 发送验证码
func (s Sms) SendSmsCode(c *gin.Context) {
	req := sms.SendSmsCodeReq{}
	sms := smsService.Sms{}
	err := s.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&sms.Service).
		Errors
	if err != nil {
		s.Logger.Error(err)
		s.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = sms.SendSmsCode(&req)
	if err != nil {
		s.Error(apis.SendSmsCodeError.Code, err, fmt.Sprintf("发送短信验证码，\r\n失败信息 %s", err.Error()))
		return
	}
	s.OK("", "发送短信验证码成功")
}

// CheckSmsCode 校验验证码
func (s Sms) CheckSmsCode(c *gin.Context) {
	req := sms.CheckSmsCodeReq{}
	sms := smsService.Sms{}
	err := s.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&sms.Service).
		Errors
	if err != nil {
		s.Logger.Error(err)
		s.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = sms.CheckSmsCode(&req)
	if err != nil {
		s.Error(apis.CheckSmsCodeError.Code, err, fmt.Sprintf(err.Error()))
		return
	}
	s.OK("", "校验短信验证码成功")
}

func (s Sms) SendEmailCode(c *gin.Context) {
	req := sms.SendSmsCodeReq{}
	sms := smsService.Sms{}
	err := s.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&sms.Service).
		Errors
	if err != nil {
		s.Logger.Error(err)
		s.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = sms.SendSmsCode(&req)
	if err != nil {
		s.Error(apis.SendSmsCodeError.Code, err, fmt.Sprintf("发送短信验证码，\r\n失败信息 %s", err.Error()))
		return
	}
	s.OK("", "发送短信验证码成功")
}
